package service

import (
	"github.com/gin-gonic/gin"
	"talentsystem/model"
	"github.com/astaxie/beego/orm"
)

func InsertGrade(c*gin.Context){
	var param model.TypeInsertGrade
	if err:=c.Bind(&param);err!=nil{
		c.JSON(400,gin.H{
			"status":400,
			"msg":"参数格式不正确",
		})
		return
	}
	if param.RecommendCode==""{
		c.JSON(200,gin.H{
			"status":200,
			"msg":1,
		})
		return
	}
	var msg model.AccountHeadhunters
	err:=orm.NewOrm().Raw("SELECT id FROM account_headhunters WHERE phone=? OR id=? ",param.RecommendCode,param.RecommendCode).QueryRow(&msg)
	if err!=nil{
		c.JSON(500,gin.H{
			"status":500,
			"msg":"系统错误请刷新重试",
		})
		return
	}
	if msg.Id==0 {
		c.JSON(403,gin.H{
			"status":403,
			"msg":0,
		})
		return
	}else{
		var result model.Grade
		num,err:=orm.NewOrm().QueryTable("grade").Filter("account_id",msg.Id).Count()
		if err!=nil{
			c.JSON(500,gin.H{
				"status":500,
				"msg":"系统错误请刷新重试",
			})
			return
		}
		if num >0{
			err=orm.NewOrm().QueryTable("grade").Filter("account_id",msg.Id).One(&result)
			if err!=nil{
				c.JSON(500,gin.H{
					"status":500,
					"msg":"系统错误请刷新重试",
				})
				return
			}
			grade:=new(model.Grade)
			grade.AccountId=param.AccountId
			grade.TeamId=msg.Id
			grade.GroupId=result.TeamId
			grade.CreateTime=param.Date
			_,err:=orm.NewOrm().Insert(grade)
			if err!=nil{
				c.JSON(500,gin.H{
					"status":500,
					"msg":"系统错误请刷新重试",
				})
				return
			}
		}else if num==0{
			grade:=new(model.Grade)
			grade.AccountId=param.AccountId
			grade.TeamId=msg.Id
			grade.CreateTime=param.Date
			_,err:=orm.NewOrm().Insert(grade)
			if err!=nil{
				c.JSON(500,gin.H{
					"status":500,
					"msg":"系统错误请刷新重试",
				})
				return
			}
		}else{
			c.JSON(403,gin.H{
				"status":403,
				"msg":3,
			})
			return
		}
	}
	c.JSON(200,gin.H{
		"status":200,
		"msg":1,
	})
	return
}

func AHHGrade(c*gin.Context){
	var param model.TypeId
	if err:=c.Bind(&param);err!=nil{
		c.JSON(400,gin.H{
			"status":400,
			"msg":"参数格式不正确",
		})
		return
	}
	var msg []model.AHHGrade
	num,err:=orm.NewOrm().Raw("SELECT DISTINCT (account_headhunters.user_name),account_headhunters.pic_url,account_headhunters.phone,grade.create_time FROM grade,account_headhunters WHERE grade.group_id=? AND account_headhunters.id=grade.account_id ORDER BY grade.id DESC",param.Id).QueryRows(&msg)
	if err!=nil {
		c.JSON(500,gin.H{
			"status":500,
			"msg":"系统错请刷新重试",
		})
		return
	}
	var result []model.AHHGrade
	total,err:=orm.NewOrm().Raw("SELECT account_headhunters.pic_url,account_headhunters.user_name,account_headhunters.phone,grade.create_time FROM grade,account_headhunters WHERE grade.team_id=? AND account_headhunters.id=grade.account_id ORDER BY grade.id DESC",param.Id).QueryRows(&result)
	if err!=nil {
		c.JSON(500,gin.H{
			"status":500,
			"msg":"系统错请刷新重试",
		})
		return
	}
	var ahhlog model.AHHLog
	for range msg{
		ahhlog.SecondOrder=msg
	}
	for range result{
		ahhlog.FirstOrder=result
	}

	c.JSON(200,gin.H{
		"status":200,
		"data":ahhlog,
		"total":num+total,
	})
	return
}